package com.hemaapp.yjnh.activity;

import java.util.ArrayList;

import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;
import xtom.frame.view.XtomRefreshLoadmoreLayout.OnStartListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.hm_FrameWork.view.RefreshLoadmoreLayout;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.CashAdapter;
import com.hemaapp.yjnh.bean.Cash;
import com.hemaapp.yjnh.bean.User;

/**
 * 提现记录
 * */
public class CashListActivity extends BaseActivity{

	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;
	private RefreshLoadmoreLayout layoutRefresh;
	private XtomListView listview;
	private LinearLayout ll_return_top;

	private CashAdapter cashAdapter;
	private ArrayList<Cash> cashs = new ArrayList<Cash>();
	private int page = 0;
	private User user;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_cmn_refresh_list_title);
		user = getApplicationContext().getUser();
		super.onCreate(savedInstanceState);
		getNetWorker().cashList(user.getToken(), page+"");
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CASH_LIST:
			showProgressDialog("请稍候");
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CASH_LIST:
			cancelProgressDialog();
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CASH_LIST:
			String page = netTask.getParams().get("page");
			HemaPageArrayResult<Cash> cResult = (HemaPageArrayResult<Cash>) baseResult;
			ArrayList<Cash> cashs = cResult.getObjects();
			if ("0".equals(page)) {// 刷新
				layoutRefresh.refreshSuccess();
				layoutRefresh.setLoadmoreable(true);
				this.cashs.clear();
				this.cashs.addAll(cashs);
			} else {// 更多
                layoutRefresh.loadmoreSuccess();
				if (cashs.size() > 0)
					this.cashs.addAll(cashs);
				else {
                    layoutRefresh.setLoadmoreable(false);
					XtomToastUtil.showShortToast(mContext, "已经到最后啦");
				}
			}
			freshData();
			break;
		default:
			break;
		}
	}

	private void freshData() {
		if (cashAdapter == null) {
			cashAdapter = new CashAdapter(mContext, cashs);
			cashAdapter.setEmptyString("暂无提现记录");
            listview.setAdapter(cashAdapter);
		} else {
			cashAdapter.notifyDataSetChanged();
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CASH_LIST:
            String page = netTask.getParams().get("page");
            if ("0".equals(page)) {// 刷新
                layoutRefresh.refreshFailed();
                freshData();
            } else {// 更多
                layoutRefresh.loadmoreFailed();
            }
            XtomToastUtil.showShortToast(mContext, "获取数据失败,"+baseResult.getMsg());
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CASH_LIST:
            String page = netTask.getParams().get("page");
            if ("0".equals(page)) {// 刷新
                layoutRefresh.refreshFailed();
                freshData();
            } else {// 更多
                layoutRefresh.loadmoreFailed();
            }
            XtomToastUtil.showShortToast(mContext, "获取数据失败,请稍后再试");
			break;
		}
	}

	@Override
	protected void findView() {
        titleText = (TextView) findViewById(R.id.title_text);
        titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
        titleRight = (Button) findViewById(R.id.title_right_btn);

        layoutRefresh = (RefreshLoadmoreLayout) findViewById(R.id.refreshLoadmoreLayout);
        listview = (XtomListView) findViewById(R.id.listview);
        ll_return_top = (LinearLayout) findViewById(R.id.ll_return_top);//回到顶部
	}

	@Override
	protected void setListener() {
		titleText.setText("提现记录");
		titleLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleRight.setVisibility(View.GONE);
		layoutRefresh.setOnStartListener(new OnStartListener() {

			@Override
            public void onStartRefresh(XtomRefreshLoadmoreLayout v) {
                page = 0;
                getNetWorker().cashList(user.getToken(), page+"");
            }

            @Override
            public void onStartLoadmore(XtomRefreshLoadmoreLayout v) {
                page++;
                getNetWorker().cashList(user.getToken(), page+"");
            }
		});
		ll_return_top.setVisibility(View.GONE);
		listview.setDividerHeight(0);
	}

	@Override
	protected void getExras() {
	}


}
